﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 图书管理系统
{
    public partial class UserManageForm : Form
    {
        public int CurEditIndex = -1;
        private string username;
        private string userid;
        private string usersex;
        private string usercollage;
        private string userpower;
        private string userface;
        private string userpwd;
        private string operation;
        private string strsql;
        private string oldID;

        public UserManageForm()
        {
            InitializeComponent();
        }

        private void UserManageForm_Load(object sender, EventArgs e)
        {
            UserNametextBox.Enabled = false;
            UserIdtextBox.Enabled = false;
            UserSextextBox.Enabled = false;
            UserCollagetextBox.Enabled = false;
            UserPowertextBox.Enabled = false;
            UserFacetextBox.Enabled = false;
            UserPwdtextBox.Enabled = false;
            Addbutton.Enabled = false;
            Changebutton.Enabled = false;
            Deletebutton.Enabled = false;
            UserdataGridView.Enabled = false;
        }

        private void StartSQLbutton_Click(object sender, EventArgs e)
        {
            UserNametextBox.Enabled = true;
            UserIdtextBox.Enabled = true;
            UserSextextBox.Enabled = true;
            UserCollagetextBox.Enabled = true;
            UserPowertextBox.Enabled = true;
            UserPwdtextBox.Enabled = true;
            Addbutton.Enabled = true;
            Changebutton.Enabled = true;
            Deletebutton.Enabled = true;
            UserdataGridView.Enabled = true;

            strsql = "select name, uid, sex, collage, power, facepath, pwd from MemberInfo";
            SqlConnection con = Utils.SQLTool.GetCon();
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter(strsql, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            UserdataGridView.DataSource = dt;
            con.Close();
        }

        private void UserdataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                CurEditIndex = e.RowIndex;
                UserNametextBox.Text = UserdataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
                UserIdtextBox.Text = UserdataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
                UserSextextBox.Text = UserdataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();
                UserCollagetextBox.Text = UserdataGridView.Rows[e.RowIndex].Cells[3].Value.ToString();
                UserPowertextBox.Text = UserdataGridView.Rows[e.RowIndex].Cells[4].Value.ToString();
                UserFacetextBox.Text = UserdataGridView.Rows[e.RowIndex].Cells[5].Value.ToString();
                UserPwdtextBox.Text = UserdataGridView.Rows[e.RowIndex].Cells[6].Value.ToString();
                oldID = UserIdtextBox.Text;
            }
        }

        private bool SearchIdSql()
        {
            userid = UserIdtextBox.Text;
            strsql = "select uid from MemberInfo where uid = '" + userid + "'";
            SqlConnection con = Utils.SQLTool.GetCon();
            con.Open();
            SqlCommand cmd = new SqlCommand(strsql, con);
            cmd.CommandText = strsql;
            object obj = cmd.ExecuteScalar();
            if (obj == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool SearchSql()
        {
            userid = UserIdtextBox.Text;
            strsql = "select * from MemberInfo";
            SqlConnection con = Utils.SQLTool.GetCon();
            con.Open();
            SqlCommand cmd = new SqlCommand(strsql, con);
            cmd.CommandText = strsql;
            object obj = cmd.ExecuteScalar();
            if (obj != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void ChangeSql()
        {
            username = UserNametextBox.Text;
            userid = UserIdtextBox.Text;
            usersex = UserSextextBox.Text;
            usercollage = UserCollagetextBox.Text;
            userpower = UserPowertextBox.Text;
            userface = UserFacetextBox.Text;
            userpwd = UserPwdtextBox.Text;
            switch (operation)
            {
                case "Add":
                    strsql = "insert into MemberInfo(name, uid, sex, collage, power, facepath, pwd)" +
                        " values('" + username + "','" + userid + "','" + usersex + "','" + 
                        usercollage + "','" + userpower + "','" + userface + "','" + userpwd + "')";
                    break;
                case "Change":
                    try
                    { 
                        strsql = "update MemberInfo set name='" + username + "'" + ",sex='" + usersex + "'" + ",collage='" + usercollage + "'" +
                        ",power='" + userpower + "'" + ",facepath='" + userface + "'"
                        + ",pwd='" + userpwd + "'" + "where uid ='" + oldID + "'";
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    break;
                case "Delete":
                    strsql = "delete from MemberInfo where uid = '" + userid + "'";
                    break;
            }
            SqlConnection con = Utils.SQLTool.GetCon();
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = strsql;
            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(userid) && !string.IsNullOrEmpty(usersex) && 
                !string.IsNullOrEmpty(usercollage) && !string.IsNullOrEmpty(userpower) &&
                !string.IsNullOrEmpty(userpwd))
                try
                {
                    int count = cmd.ExecuteNonQuery();
                    if (count > 0)
                    {
                        UserNametextBox.Text = "";
                        UserIdtextBox.Text = "";
                        UserSextextBox.Text = "";
                        UserCollagetextBox.Text = "";
                        UserPowertextBox.Text = "";
                        UserFacetextBox.Text = "";
                        UserPwdtextBox.Text = "";
                        MessageBox.Show("执行成功！");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("注册失败, " + ex.Message);
                }
            else
            {
                MessageBox.Show("信息为空格或空，执行错误！");
                UserNametextBox.Text = "";
                UserIdtextBox.Text = "";
                UserSextextBox.Text = "";
                UserCollagetextBox.Text = "";
                UserPowertextBox.Text = "";
                UserFacetextBox.Text = "";
                UserPwdtextBox.Text = "";
            }
        }

        private void Addbutton_Click(object sender, EventArgs e)
        {
            operation = "Add";
            if (SearchIdSql())
            {
                ChangeSql();
            }
            else
            {
                MessageBox.Show("用户编号已被注册，执行错误！");
            }
        }

        private void Changebutton_Click(object sender, EventArgs e)
        {
            operation = "Change";
            if (SearchSql())
            {
                if (!SearchIdSql())
                {
                    ChangeSql();
                }
                else
                    MessageBox.Show("只能修改除账号与人脸以外的信息！");
            }
            else
            {
                MessageBox.Show("数据库为空，执行错误！");
            }
        }

        private void Deletebutton_Click(object sender, EventArgs e)
        {
            operation = "Delete";
            if (SearchSql())
            {
                ChangeSql();
            }
            else
            {
                MessageBox.Show("数据库为空，执行错误！");
            }
        }

        private void Returnbutton_Click(object sender, EventArgs e)
        {
            Utils.SQLTool.CloseSql();
            this.Close();
        }
    }
}
